package com.ksc.storekeeper.client.view.validation;

import com.ksc.storekeeper.client.resources.StoreKeeperResources;
import com.ksc.storekeeper.client.utils.RegexUtil;

public class ValidatingTelecomHelper<T> {

	private boolean required = false;
	// private int minRequired = 0;
	// private int maxAllowed = 0;
	// private boolean numeric = false;
	public String errorMessage = "";

	private final IsTelecomValidatingWidget<T> validatingWidget;

	public ValidatingTelecomHelper(IsTelecomValidatingWidget<T> validatingWidget) {
		this.validatingWidget = validatingWidget;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	private void addError() {
		validatingWidget.asWidget().addStyleName(StoreKeeperResources.INSTANCE.storeKeeperCss().error());
	}

	/**
	 * For NT Landline Number
	 * 
	 * @return
	 */
	public Validation validateLandLine() {
		if (required && validatingWidget.getText().isEmpty()) {
			return getValidationMessage(Validation.REQUIRED);
		}
		if (required && validatingWidget.getText().length() == 8) {
			if (!validatingWidget.getText().matches("^[0-9]+$")) {
				return getValidationMessage(Validation.INVALID_LANDLINE_NUMBER);
			}
		} else {
			return getValidationMessage(Validation.INVALID_LANDLINE_NUMBER_LENGTH);
		}

		return removeInvalid();
	}

	/**
	 * For ADSL
	 * 
	 * @return
	 */
	public Validation validateAdslUsername() {
		if (required && validatingWidget.getText().isEmpty()) {
			return getValidationMessage(Validation.REQUIRED);
		}

		return removeInvalid();
	}

	public Validation validateMobileNumber() {
		if (required && validatingWidget.getText().isEmpty()) {
			return getValidationMessage(Validation.REQUIRED);
		}
		if (required && !RegexUtil.isMobileNumber(validatingWidget.getText())) {
			return getValidationMessage(Validation.INVALID_MOBILE_NUMBER);
		}
		return removeInvalid();
	}

	public Validation validateContactNumber() {
		if (required && validatingWidget.getText().isEmpty()) {
			return getValidationMessage(Validation.REQUIRED);
		}
		if (required && !RegexUtil.isContactNumber(validatingWidget.getText())) {
			return getValidationMessage(Validation.INVALID_CONTACT_NUMBER);
		}
		return removeInvalid();
	}

	public Validation validateGSMNumber() {
		if (required && validatingWidget.getText().isEmpty()) {
			return getValidationMessage(Validation.REQUIRED);
		}
		if (required && !RegexUtil.isGSMNumber(validatingWidget.getText())) {
			return getValidationMessage(Validation.INVALID_GSM_NUMBER);
		}
		return removeInvalid();
	}

	public Validation validateCDMANumber() {
		if (required && validatingWidget.getText().isEmpty()) {
			return getValidationMessage(Validation.REQUIRED);
		}
		if (required && !RegexUtil.isCDMANumber(validatingWidget.getText())) {
			return getValidationMessage(Validation.INVALID_CDMA_NUMBER);
		}
		return removeInvalid();
	}

	public Validation validateGSMPostPaidNumber() {
		if (required && validatingWidget.getText().isEmpty()) {
			return getValidationMessage(Validation.REQUIRED);
		}
		if (required && !RegexUtil.isGSMPostPaidNumber(validatingWidget.getText())) {
			return getValidationMessage(Validation.INVALID_GSM_POSTPAID_NUMBER);
		}
		return removeInvalid();
	}

	public Validation validateNcellPostpaidNumber() {
		if (required && validatingWidget.getText().isEmpty()) {
			return getValidationMessage(Validation.REQUIRED);
		}
		if (required && !RegexUtil.isNCellPostpaidNumber(validatingWidget.getText())) {
			return getValidationMessage(Validation.INVALID_NCELL_POSTPAID_NUMBER);
		}
		return removeInvalid();
	}

	public Validation validateCDMAPostPaidNumber() {
		if (required && validatingWidget.getText().isEmpty()) {
			return getValidationMessage(Validation.REQUIRED);
		}
		if (required && !RegexUtil.isCDMAPostPaidNumber(validatingWidget.getText())) {
			return getValidationMessage(Validation.INVALID_CDMA_POSTPAID_NUMBER);
		}
		return removeInvalid();
	}

	public void showInvalid() {
		validatingWidget.asWidget().addStyleName(StoreKeeperResources.INSTANCE.storeKeeperCss().error());
	}

	private Validation getValidationMessage(Validation validation) {
		addError();
		errorMessage = ErrorMessages.getErrorMessage(validation.toString());
		return validation;
	}

	public Validation removeInvalid() {
		errorMessage = "";
		validatingWidget.asWidget().removeStyleName(StoreKeeperResources.INSTANCE.storeKeeperCss().error());
		return Validation.VALID;
	}

	public void setNumeric(boolean numeric) {
		// this.numeric = numeric;
	}

	public void setMinLength(int length) {
		// this.minRequired = length;
	}

	public void setMaxLength(int length) {
		// this.maxAllowed = length;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

}